﻿using System.ComponentModel;
using K9Nano.Application.Authentication;
using K9Nano.Authorization;
using K9Nano.Domain.Entities;
using K9Nano.Exceptions;
using K9Nano.Mapping;
using Microsoft.AspNetCore.Mvc;

namespace K9Nano.AspNetCore.Controllers;

public abstract class UserControllerBase<TUser, TService, TCreateDto, TEditDto, TItemDto, TQueryDto>(TService service)
    : CurdControllerBase<TUser, TService, TCreateDto, TEditDto, TItemDto, TQueryDto>(service)
    where TUser : UserBase
    where TService : IUserAppServiceBase<TUser, TCreateDto, TEditDto, TItemDto, TQueryDto>
    where TCreateDto : UserCreateDto
    where TEditDto : UserEditDto
    where TItemDto : UserDto
    where TQueryDto : UserQueryDto<TUser>
{
    /// <summary>
    /// 获取用户的权限列表
    /// </summary>
    [HttpGet("permissions/{userId}")]
    [Description("获取用户的权限列表")]
    public virtual Task<IEnumerable<Permission>> GetPermissionsByUserId(long userId)
        => service.GetPermissionsByUserIdAsync(userId, HttpContext.RequestAborted);

    /// <summary>
    /// 获取用户的角色列表
    /// </summary>
    [HttpGet("roles/{userId}")]
    [Description("获取用户的角色列表")]
    public virtual Task<IEnumerable<RoleDto>> GetRolesByUserId(long userId)
        => service.GetRolesByUserIdAsync(userId, HttpContext.RequestAborted);

    /// <summary>
    /// 为用户批量添加角色
    /// </summary>
    [HttpPost("add/roles")]
    [Description("为用户批量添加角色")]
    public virtual Task AddRoles(UserRoleEditDto dto)
        => service.AddRolesAsync(dto, HttpContext.RequestAborted);

    /// <summary>
    /// 为用户批量删除角色
    /// </summary>
    [HttpPost("remove/roles")]
    [Description("为用户批量删除角色")]
    public virtual Task RemoveRoles(UserRoleEditDto dto)
        => service.RemoveRolesAsync(dto, HttpContext.RequestAborted);

    /// <summary>
    /// 为用户重置密码
    /// </summary>
    [HttpPut("password")]
    [Description("为用户重置密码")]
    public virtual async Task<bool> ResetPassword([FromBody] UserResetPasswordDto dto)
    {
        if (string.IsNullOrWhiteSpace(dto.Password)) throw FriendlyException.BadRequest("密码不能为空");
        if (dto.Password.Length < 8) throw FriendlyException.BadRequest("密码长度不能小于8");
        return await service.ResetPasswordAsync(dto, HttpContext.RequestAborted);
    }

    /// <summary>
    /// 修改用户组织
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPut("organization")]
    public virtual async Task SetOrganization(ChangeUserOrganizationDto dto)
    {
        if(dto.UserId < 0 || dto.OrganizationId < 0) throw FriendlyException.BadRequest("参数不合法");
        await service.SetOrganizationAsync(dto, HttpContext.RequestAborted);
    }

    /// <summary>
    /// 获取组织下的用户列表
    /// </summary>
    /// <param name="organizationId">组织ID</param>
    /// <returns></returns>
    [HttpGet("organization/{organizationId}")]
    public virtual async Task<TItemDto[]> GetUsersByOrganizantionId(long organizationId)
    {
        return await service.GetUsersByOrganizantionIdAsync(organizationId, HttpContext.RequestAborted);
    }
}
